package org.redr.widgetGUI.settings;

import java.util.HashMap;

import org.redr.widgetGUI.core.LayoutState;

import com.trolltech.qt.gui.QHBoxLayout;
import com.trolltech.qt.gui.QLayout;
import com.trolltech.qt.gui.QSizePolicy;
import com.trolltech.qt.gui.QVBoxLayout;
import com.trolltech.qt.gui.QWidget;

/**
 * <p>
 * Base widget for all Red-R qtderived widgets.  WidgetStateChildren all have a layout 
 * element which represents the layout of child widgets on that parent.  WidgetStateChildren also 
 * may have one or more widget elements or ways to access them allowing developers to manipulate
 * each qtWidget individually.</p>
 * <p>
 * The main idea of the core widgets is to use the WidgetSettings class to control 
 * settings of the widget or to use widget accessor functions to collect information
 * from the qtderived widgets.  
 * @author Kyle R Covington
 *
 */
public class WidgetState {
	protected LayoutState 			layout;
	protected QWidget			widgetBox;
	protected WidgetSettings	settings;
	
	public QLayout getLayout(){
		return widgetBox.layout();
	}
	
	private void _setStateSettings(){
		_setStateSettings(null);
	}
	
	private void _setStateSettings(QWidget parent){
		if (parent == null){
			widgetBox = new QWidget(){
//				@Override
//				public void mousePressEvent(QMouseEvent ev){
//		    		System.out.println("Clicked A widget State");
//		    		System.out.println(this.sizePolicy().toString());
//					super.mousePressEvent(ev);
//		    		// activated.emit(indexAt(ev.pos()));
//		    	}
			};
		} else {
			widgetBox = new QWidget(parent){
//				@Override
//				public void mousePressEvent(QMouseEvent ev){
//		    		System.out.println("Clicked A widget State");
//		    		System.out.println(this.sizePolicy().toString());
//					super.mousePressEvent(ev);
//		    		// activated.emit(indexAt(ev.pos()));
//		    	}
			};
		}
		widgetBox.setLayout(_getLayout());
		widgetBox.layout().setWidgetSpacing(0);
		widgetBox.layout().setContentsMargins(1, 1, 1, 1);
//		int minWidth 	= 	Integer.parseInt(_getSetting("minWidth", "-1"));
//		if (minWidth > 0)	widgetBox.setMinimumWidth(minWidth);
//		int minHeight	=	Integer.parseInt(_getSetting("minHeight", "-1"));
//		if (minHeight > 0)	widgetBox.setMinimumHeight(minHeight);
//		int maxWidth	=	Integer.parseInt(_getSetting("maxWidth", "-1"));
//		if (maxWidth > 0)	widgetBox.setMaximumWidth(maxWidth);
//		int maxHeight	=	Integer.parseInt(_getSetting("maxHeight", "-1"));
//		if (maxHeight > 0)	widgetBox.setMaximumHeight(maxHeight);
		widgetBox.setEnabled(Boolean.parseBoolean(_getSetting("enabled", "true")));
		widgetBox.setHidden(Boolean.parseBoolean(_getSetting("hidden", "false")));
		widgetBox.setSizePolicy(QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Minimum);
		widgetBox.show();
	}
	
	public WidgetState(QLayout l, WidgetSettings ws){
		settings = ws;
		_setStateSettings(l.parentWidget());
		l.addWidget(widgetBox);
	}
	
	public WidgetState(LayoutState parent, WidgetSettings ws){
		settings = ws;
		layout	=	parent;
		_setStateSettings(parent.widgetBox);
		parent.addWidget(widgetBox);
	}
	
	public WidgetState(WidgetSettings ws){
		settings = ws;
		_setStateSettings();
	}
	
	private QLayout _getLayout(){
		String ls = _getSetting("layout", "vertical");
		if (ls.equals("horizontal")) return new QHBoxLayout();
		else return new QVBoxLayout();
	}
	
	public QWidget getWidgetBox(){
		return widgetBox;
	}
	
//	public void addWidget(WidgetState ws){
//		System.out.println("Adding widget to the layout");
//		addWidget(ws.getWidgetBox());
//	}
//	
//	public void addWidget(QWidget w){
//		w.setParent(layout.getWidgetBox());
//		layout.addWidget(w);
//	}
	
	protected String _getSetting(String key, String def){
		String setting = settings.getSetting(key);
		if(setting == null){
			settings.setSetting(key, def);
			return def;
		}
		else return setting;
	}
	
	protected void _setSetting(String key, String value){
		settings.setSetting(key, value);
	}
	
	public HashMap<String, String> items(String group){
		return settings.items(group).getKeyValuePairs();
	}
	
	public void hide(){
		widgetBox.hide();
	}
	
	public void show(){
		widgetBox.show();
	}
	
	public void delete(){
		widgetBox.dispose();
	}
	
	public String _(String s){
		return s;
	}
	
	public void setEnabled(boolean state){
		this.getWidgetBox().setEnabled(state);
		this._setSetting("enabled", String.valueOf(state));
	}
	
	public void setHidden(boolean state){
		this.getWidgetBox().setHidden(state);
		this._setSetting("hidden", String.valueOf(state));
	}
	
	

}
